package tmproject.hlhj.fhp.cartrans.activitys

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Handler
import android.os.Message
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.view.View
import kotlinx.android.synthetic.main.aty_login.*
import tmproject.hlhj.fhp.cartrans.IView.LoginView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.presenter.LoginPresenter
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.LoginBean
import tmproject.hlhj.fhp.cartrans.utils.FormatUtil
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils

/**
 * Created by Administrator on 2018/8/11/011.
 */
class LoginAty : BaseAty<LoginView, LoginPresenter>(), LoginView {
    private var sec=60
    private var isDeling=false
    companion object {
        var phoneStr=""
    }
    private val smsHandler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec>0){
                sec--
                imgSendSms.text="${sec}s后重发"
                imgSendSms.setBackgroundResource(R.drawable.sms_border_1)
                btSendSms.isClickable=false
                this.sendEmptyMessageDelayed(1,1000)
            }else{
                imgSendSms.setBackgroundResource(R.drawable.sms_border_2)
                btSendSms.isClickable=true
                imgSendSms.text="发送验证码"
                isDeling=false
                judeState()
            }
        }
    }
    /**
     * 登录回调
     */
    override fun loginResp(loginBean: LoginBean) {
        SpHelper.saveToken(this,loginBean.data.token)
        startActivityForResult(Intent(this@LoginAty,HomeAty::class.java),1)
        finish()
    }

    override fun initView() {
        etPhone.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
    }
    override fun logicStart() {
        if (null!=SpHelper.getToken(this)&&!SpHelper.getToken(this).equals("")){
            startActivity(Intent(this@LoginAty,HomeAty::class.java))
            finish()
        }
    }

    override fun setListener() {
        /*按钮点击事件*/
        btSendSms.setOnClickListener {
            if (FormatUtil.isMobileNO(etPhone.text.toString())){
                presenter?.sendSms(this@LoginAty,etPhone.text.toString())
            }else{
                ToastUtils.toast("手机号格式不正确")
            }
        }

        etPhone.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                if (etPhone.text.isNotEmpty()){
                    imgDel.visibility=View.VISIBLE
                    btDelet.isClickable=true
                }else{
                    imgDel.visibility=View.GONE
                    btDelet.isClickable=false
                }

                judeState()
            }


            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })
        etYzm.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                judeState()
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })
        btLogin.setOnClickListener {
            phoneStr=etPhone.text.toString()
            presenter?.login(this,etPhone.text.toString(),etYzm.text.toString())
        }
        btRegis.setOnClickListener {
            val intent = Intent(this@LoginAty, RegisAty::class.java)
            startActivity(intent)
        }
    }
    //判断按钮状态
    private fun judeState() {
        btDelet.setOnClickListener {
            etPhone.setText("")
        }
        if (!isDeling){
            if (FormatUtil.isMobileNO(etPhone.text.toString())){
                imgSendSms.setBackgroundResource(R.drawable.sms_border_2)
                imgSendSms.setTextColor(Color.parseColor("#ffffff"))
                btSendSms.isClickable=true
            }else{
                imgSendSms.setTextColor(Color.parseColor("#333333"))
                imgSendSms.setBackgroundResource(R.drawable.sms_border_1)
                btSendSms.isClickable=false
            }
        }
        if (etPhone.text.isEmpty() || etYzm.text.isEmpty()) {
            btLogin.alpha = 0.5f
            btLogin.isClickable = false

        } else {

            btLogin.alpha = 1f
            btLogin.isClickable = true
        }
    }

    override fun bindView(): LoginView {
        return this
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun getContentId(): Int {

        return R.layout.aty_login
    }

    /**
     * 发送验证码回调
     */
    override fun sendSmsResp(baseBean: BaseBean) {
        if (baseBean.code==200){
            isDeling=true
//            ToastUtils.toast("发送成功")
            smsHandler.sendEmptyMessage(1)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        smsHandler.removeMessages(1)
    }
}